package com.iot.ammeter.view.fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import com.iot.ammeter.IApplication
import com.iot.ammeter.R
import com.iot.ammeter.common.noData
import com.iot.ammeter.common.rmbString
import com.iot.ammeter.databinding.FragmentMyDeviceBinding
import com.iot.ammeter.model.parcelable.ChargeStatus
import com.iot.ammeter.model.parcelable.DeviceStatus
import com.iot.ammeter.view.DeviceBindActivity
import com.iot.ammeter.viewmodel.MainViewModel
import java.util.*

class MyDeviceFragment : Fragment() {

    private lateinit var _binding: FragmentMyDeviceBinding
    private val binding: FragmentMyDeviceBinding get() = _binding

    private val mainViewModel by activityViewModels<MainViewModel>()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        // Inflate the layout for this fragment with fragment binding
        return FragmentMyDeviceBinding.inflate(inflater, container, false).also {
            _binding = it
        }.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // init card UI and data display
        val deviceInfo = IApplication.deviceInfo
        val lastMonthInfo = IApplication.lastMonthInfo!!

        binding.apply {
            // 配置用电量数据
            val status = if (deviceInfo == null) {
                dailyElectricityCard.content = electricDefault()
                monthElectricityCard.content = electricDefault()
                DeviceStatus.NO_DEVICE
            } else deviceInfo.run {
                dailyElectricityCard.content = electricString(deviceInfo.electricDaily)
                monthElectricityCard.content = electricString(electricMonth)
                status
            }

            // 配置设备状态卡数据
            deviceStatusCard.apply {
                title = deviceStatusString(status)
                titleColor(status.color)
                iconColor = status.color
                icon(status.icon)
                content = if (status == DeviceStatus.NO_DEVICE)
                    getString(R.string.pleaseBindTheDeviceAgain)
                else deviceInfo!!.location.toString()
            }

            // 配置上月电费卡数据
            val chargeStatus = getChargeStatus(lastMonthInfo.charge.status)
            chargeCard.apply {
                content = rmbString(lastMonthInfo.charge.amount)
                hintTag = getString(chargeStatus.msgResId)
                hintTagColor(chargeStatus.color)
                contentColor(chargeStatus.color)
                iconColor = chargeStatus.color
                icon(chargeStatus.iconResId)
            }

            // 缴费按钮事件
            paymentButton.setOnClickListener {  }

            deviceRebindButton.setOnClickListener {
                launchDeviceBindActivity()
            }
        }
    }

    private fun launchDeviceBindActivity() {
        DeviceBindActivity.launchActivity(requireContext())
    }

    private fun electricDefault() : String = noData

    /**
     * 将用电量和用电量单位拼接为字符串，如果是浮点类型保留小数点后两位
     * @see R.string.electricity_unit
     */
    private fun electricString(electricity: Number?): String = "${
        if (electricity is Float || electricity is Double)
            String.format("%.2f", electricity)
        else electricity
    }${getString(R.string.electricity_unit)}"

    /**
     * 通过枚举类型设备状态转换为字符串
     * @see DeviceStatus
     */
    private fun deviceStatusString(status: DeviceStatus): String =
        (if (status == DeviceStatus.NO_DEVICE) ""
        else "${getString(R.string.deviceStatusTitle)}：") +
                getString(status.title)


    /**
     * 根据支付状态和当前日期获取对应的 [ChargeStatus]
     */
    private fun getChargeStatus(statue: Boolean): ChargeStatus = if (statue) ChargeStatus.Finished
    else {
        val calendar = Calendar.getInstance().apply {
            time = Date()
        }
        if (calendar.get(Calendar.DATE) < 10)
            ChargeStatus.Unfinished
        else ChargeStatus.Overdue
    }
}